package com.apes.fico.engine.repository;

import com.apes.fico.engine.model.ReduceVoucher;
import com.apes.framework.jpa.repository.CustomRepository;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface ReduceVoucherRepository extends CustomRepository<ReduceVoucher,String> {

    @Query(
            value = "select t from ReduceVoucher t where t.company.id = :companyId and t.voucherTemplate.id = :voucherTemplateId and t.remark = :remark" +
                    " and t.accountingVoucherType.id = :accountingVoucherTypeId and t.cycleValue = :cycleValue and t.cycle = :cycle"
    )
    ReduceVoucher findUniqueVoucher(@Param("companyId") String companyId, @Param("voucherTemplateId") String voucherTemplateId, @Param("remark") String remark,
                                                        @Param("accountingVoucherTypeId") String accountingVoucherTypeId, @Param("cycleValue") String cycleValue, @Param("cycle") String cycle);

    @Query(
            value = "select t from ReduceVoucher t where t.company.id = :companyId and t.voucherTemplate.id = :voucherTemplateId and t.remark = :remark" +
                    " and t.accountingVoucherType.id = :accountingVoucherTypeId and t.cycleValue = :cycleValue and t.cycle = :cycle and t.done = false"
    )
    ReduceVoucher findUniqueVoucherIsNotDone(@Param("companyId") String companyId, @Param("voucherTemplateId") String voucherTemplateId, @Param("remark") String remark,
                                                        @Param("accountingVoucherTypeId") String accountingVoucherTypeId, @Param("cycleValue") String cycleValue, @Param("cycle") String cycle);


    @Query(
            value = "select t.id from ReduceVoucher t where t.done = false and t.haveError = false and t.cycle = :cycle and t.cycleValue = :cycleValue"
    )
    List<String> findNotDoneAndNotErrorVoucherByCycleAndCycleValue(@Param("cycle") String cycle, @Param("cycleValue") String cycleValue, Pageable pageable);

}
